package com.ysh.xiaomi.dao.impl;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.ysh.xiaomi.dao.OrderDao;
import com.ysh.xiaomi.pojo.TbAddress;
import com.ysh.xiaomi.pojo.TbOrder;
import com.ysh.xiaomi.utils.DateUtils;
import com.ysh.xiaomi.utils.DbUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 袁胜虎 on 2022/8/18 19:40
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public void addOrder(String orderId, Long userId, Long addressId, Long money) {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into tb_order(id,uid,money,aid)value(?,?,?,?)";
        try {
            int update = qr.update(DbUtils.getConn(), sql, orderId, userId, money, addressId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addOrderDetail(String orderId, Long goodKzId, long num, long money) {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into tb_orderdetail values(null,?,?,?,?)";
        try {
            int update = qr.update(DbUtils.getConn(), sql, orderId, goodKzId, num, money);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<TbOrder> findByUserId(Long userId) {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_order where uid = ?";
        try {
            List<TbOrder> orders = qr.query(DbUtils.getConn(), sql, new BeanListHandler<>(TbOrder.class), userId);
            return orders;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateOrderStaus(String oid, String status) {
        QueryRunner qr = new QueryRunner();
        String sql = "update tb_order set status = ? where id = ?";
        try {
            int update = qr.update(DbUtils.getConn(), sql, status, oid);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public TbOrder findById(String orderId) {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_order where id = ?";
        try {
            TbOrder order = qr.query(DbUtils.getConn(), sql, new BeanHandler<>(TbOrder.class), orderId);
            return order;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public TbOrder findOrderById(String oid) {
        QueryRunner qr = new QueryRunner();
        String sql="SELECT tod.id,tod.money,tod.time,ta.name as name,ta.phone as phone,ta.detail as detail,td.oid as oid FROM tb_order tod left JOIN  tb_address ta on ta.id=tod.aid RIGHT JOIN tb_orderdetail td on td.Oid=tod.id  where  td.Oid = ?";

        try {
            Map<String, Object> objectMap = qr.query(DbUtils.getConn(), sql, new MapHandler(), oid);
            if (objectMap == null){
                return null;
            }
            TbOrder orders = new TbOrder();
            TbAddress address = new TbAddress();
            BeanUtils.populate(orders,objectMap);
            BeanUtils.populate(address,objectMap);
            orders.setAddress(address);

            return orders;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
